package com.shijunhao.graduation.service.scheduled;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.bean.EveryDayInfo;
import com.shijunhao.graduation.domain.bean.HotInfoKey;
import com.shijunhao.graduation.domain.entity.BusinessForum;
import com.shijunhao.graduation.domain.entity.BusinessForumThyme;
import com.shijunhao.graduation.domain.entity.BusinessHotInfo;
import com.shijunhao.graduation.mapper.BusinessForumThymeMapper;
import com.shijunhao.graduation.service.business.IBusinessForumService;
import com.shijunhao.graduation.service.business.IBusinessForumThymeService;
import com.shijunhao.graduation.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: shijunhao
 * @date: 2022/4/25 23:17
 * @description:
 */
@Service
public class ForumTypeScheduleService {

    @Resource
    private BusinessForumThymeMapper businessForumThymeMapper;
    @Resource
    private IBusinessForumService businessForumService;

    private Map<Long, EveryDayInfo> todayInfo = new ConcurrentHashMap<>();

    private Logger logger = LoggerFactory.getLogger(ForumTypeScheduleService.class);


    @Scheduled(cron = "0 0 0 * * ?")
    @PreDestroy
    public void updateInfo(){

    }


    public EveryDayInfo getInfo(Long id){
        //本地没有找到就去查
        if(this.todayInfo.get(id) == null){
            int count = businessForumThymeMapper.selectToday(id);
            int all = businessForumService.selectCount(Wrappers.lambdaQuery(BusinessForum.class).eq(BusinessForum::getThyme, id));
            EveryDayInfo info = new EveryDayInfo(count, all);
            todayInfo.put(id,info);
        }
        return this.todayInfo.get(id);
    }

    public void updateOne(Long id){
        EveryDayInfo info = null;
        if( (info = this.todayInfo.get(id)) == null){
            info = getInfo(id);
        }
        info.setDayCount(info.getDayCount()+1);
        info.setAllCount(info.getAllCount()+1);
    }

}
